<?php
namespace app\bis\controller;
use think\Controller;
use think\Db;
use think\Exception;

class Register extends Controller
{
    public function index(){
        $citys = model('city')->getCitysByParentId();
        $categorys = model('category')->getCategorysByParentId();
        $this->assign([
            'citys' => $citys,
            'categorys' => $categorys,
        ]);
        return $this->fetch();
    }

    public function add(){
        if(!request()->isPost()){
            $this->error('请求错误');
        }
        //获取表单的数据
        $data = input('post.');
        // var_dump($data);
        // $validate = validate('bis');
        // if(!$validate->scene('add')->check($data)){
        //     $this->error($validate->getError());
        // }

        //获取经纬度
        $lnglat = \Map::getLngLat($data['address']);
        // var_dump($lnglat);die;
        if(empty($lnglat) || $lnglat['status'] != 0){
            $this->error('无法获取准确地址，或地址匹配不准确，请核实地址输入是否有误');
        }

        //判断用户是否已经存在
        $accountResult = model('BisAccount')->get(['username' => $data['username']]);
        if($accountResult){
            $this->error('该用户存在，请重新设置用户名');
        }
        //开启事务
//        Db::startTrans();
        try{
            //商户基本信息入库
            $bisData = [
                'name' => $data['name'],
                'city_id' => $data['city_id'],
                'city_path' => empty($data['se_city_id']) ? $data['city_id'] : $data['city_id'].','.$data['se_city_id'],
                'logo' => $data['logo'],
                'licence_logo' => $data['licence_logo'],
                'description' => empty($data['discription']) ? '' : $data['discription'],
                'bank_info' => $data['bank_info'],
                'bank_user' => $data['bank_user'],
                'bank_name' => $data['bank_name'],
                'faren' => $data['faren'],
                'faren_tel' => $data['faren_tel'],
                'email' => $data['email'],
            ];

            $bisId = model('bis')->add($bisData);
            // var_dump($bisId);

            //总店相关信息校验
            $data['cat'] = '';

            if(!empty($data['se_category_id'])){
                $data['cat'] = implode(',',$data['se_category_id']);
            }

            //总店相关信息入库
            $locationData = [
                'bis_id' => $bisId,
                'name' => $data['name'],
                'logo' => $data['logo'],
                'tel' => $data['tel'],
                'contact' => $data['contact'],
                'category_id' => $data['category_id'],
                'category_path' => $data['category_id'].','.$data['cat'],
                'city_id' => $data['city_id'],
                'city_path' => empty($data['se_city_id']) ? $data['city_id'] : $data['city_id'].','.$data['se_city_id'],
                'api_address' => $data['address'],
                'content' => empty($data['content']) ? '' : $data['content'],
                'is_main' => 1,//代表总店信息
                'xpoint' => empty($lnglat['result']['location']['lng']) ? '' : $lnglat['result']['location']['lng'],
                'ypoint' => empty($lnglat['result']['location']['lat']) ? '' : $lnglat['result']['location']['lat'],
            ];

            $locationId = model('BisLocation')->add($locationData);

            //账户相关信息校验

            //自动生成加盐字符串
            $data['code'] = mt_rand(1000,9999);
            $accountData = [
                'bis_id' => $bisId,
                'username' => $data['username'],
                'code' => $data['code'],
                'password' => md5($data['password'].$data['code']),
                'is_main' => 1,//代表总店管理员
            ];

            $accountId = model('BisAccount')->add($accountData);
        }catch (Exception $e){
            //事务回滚
//            Db::rollback();
            return '入驻申请失败';
        }



        //发送邮件
        $url = request()->domain().url('bis/register/waiting',['id'=>$bisId]);
        $title = 'kvshop入驻申请通知';
        $content = "您提交的入驻申请需要等待管理员审核，您可以点击接链接查看<a href='".$url."'>审核进度查询</a>";
        \phpmailer\Email::send($data['email'],$title,$content);

        $this->success('申请成功', url('register/waiting',['id'=>$bisId]));
    }

    public function waiting($id){
            if(empty($id)){
                $this->error('error');
            }
            $detail =model('Bis')->get($id);
            return $this->fetch('',[
                'detail' => $detail,
            ]);
    }
}
